package com.niuke;

import com.pojo.ListNode;

/**
 * Description:
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2021-12-14 22:06:29
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-12-14     张李鑫                     1.0         1.0 Version
 */
public class JZ76 {
    /**
     * 在一个排序的链表中，存在重复的结点，请删除该链表中重复的结点，重复的结点不保留，返回链表头指针。
     * 例如，链表 1->2->3->3->4->4->5  处理后为 1->2->5
     */

    public static ListNode deleteDuplication(ListNode pHead) {
        if (pHead == null) {
            return null;
        }
        ListNode cur = pHead;
        ListNode pre = null;
        while (cur != null) {
            if (cur.next != null && cur.val == cur.next.val) {
                ListNode temp = null;
                int val = cur.val;
                while (cur != null && cur.val == val) {
                    temp = cur.next;
                    cur.next = null;
                    cur = temp;
                }
                if (pre != null) {
                    pre.next = cur;
                } else {
                    pHead = cur;
                }
            } else {
                pre = cur;
                cur = cur.next;
            }
        }
        return pHead;
    }

    public static void main(String[] args) {
        ListNode listNode = new ListNode(1);
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(2);
        ListNode listNode4 = new ListNode(3);
        ListNode listNode5 = new ListNode(3);

        listNode.next = listNode1;
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;


        System.out.println(deleteDuplication(listNode));


    }

}